package com.ybwh.springboot2.conf;


import com.ybwh.springboot2.common.mybatis.plus.update.force.MybatisPlusForceUpdater;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.Connection;
import java.sql.SQLException;

@Configuration
public class MybatisPlusForceUpdaterConfig {

    // -------------------spring下两种方式--------------------------------------------
    @Autowired
    protected SqlSessionFactory sessionFactory;

    @Bean
    public MybatisPlusForceUpdater mybatisPlusForceUpdater() {
        return new MybatisPlusForceUpdater(){
            @Override
            protected Connection currentConnection() throws SQLException {
                SqlSession sqlSession = SqlSessionUtils.getSqlSession(sessionFactory);
                return  sqlSession.getConnection();
            }
        };
    }


//    @Autowired
//    protected DataSource dataSource;
//
//
//    @Bean
//    public MybatisPlusForceUpdater mybatisPlusForceUpdater() {
//        return new MybatisPlusForceUpdater(){
//            @Override
//            protected Connection currentConnection() throws SQLException {
//                return  DataSourceUtils.getConnection(dataSource);
//            }
//        };
//    }
}
